c = int(input())
max_dl = 0
if len(str(c)) == 4:
    for i in range(2, c + 1):
        if str(i) in str(c):
            a = False
            for dl in range(2, i // 2 + 1):
                if i % dl == 0:
                    a = True
                    break
            if a:
                continue
            else:
                max_dl = max(max_dl, i)
print(max_dl)
